import Vue from 'vue'
import Router from 'vue-router'
import init from '../components/init/BMPinit.vue'
import Home from '../components/tabbar/BMPHOMEPAGE.vue'
import Message from '../components/tabbar/BMPmessage.vue'
import Jop from '../components/tabbar/BMPjop.vue'
import Personal from '../components/tabbar/BMPpersonal.vue'
import login from '../components/login/BMPlogin.vue'


Vue.use(Router)
const router = new Router({
  routes: [{
      path: '/init',
      component: init,
      children: [{
          path: '//',
          redirect: '/init/home'
        },
        {
          path: '/init/home',
          component: Home
        },
        {
          path: '/init/message',
          component: Message
        },
        {
          path: '/init/jop',
          component: Jop
        },
        {
          path: '/init/personal',
          component: Personal
        },
      ],
      name: 'init',
      meta: {
        requiresAuth: true
      }
    },
    {
      path: '/login',
      component: login
    }
  ]
})

//全局路由守卫
router.beforeEach((to, from, next) => {
  if (to.matched.some(record => record.meta.requiresAuth)) {
    if (!localStorage.getItem('name')) {
      next({
        path: '/login',
        query: {
          redirect: to.fullPath
        }
      })
    } else {
      next()
    }
  } else {
    next()
  }
})

export default router